﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using transportsystem.DTO;
using UTIL;

namespace transportsystem.DAO
{
    public class KhoHangDAO
    {
        public static DataTable getKhoHangByMaHang(int maHang)
        {
            SqlDataAccessHelper.ConnectSQLServer();
            string procName = "sp_TraKhoHangTheoMaHang";
            SqlParameter[] sp = new SqlParameter[1];
            sp[0] = new SqlParameter("@mahang", maHang);
            DataSet ds = SqlDataAccessHelper.LoadDataWithParameter(procName, sp);
            if (ds != null)
                return ds.Tables[0];
            return null;
        }

        public static int getMaKhoHangByMaHang(int maHang)
        {
            SqlDataAccessHelper.ConnectSQLServer();
            string procName = "sp_TraMaKhoHangTheoMaHang";
            SqlParameter[] sp = new SqlParameter[1];
            sp[0] = new SqlParameter("@mahang", maHang);
            DataSet ds = SqlDataAccessHelper.LoadDataWithParameter(procName, sp);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
                return int.Parse(ds.Tables[0].Rows[0]["makho"].ToString());
            return 0;
        }

        public static DataTable getAllKhoHang()
        {
            SqlDataAccessHelper.ConnectSQLServer();
            string procName = "sp_TraCuuKhoHang";
            DataSet ds = SqlDataAccessHelper.LoadData(procName);
            if (ds != null)
                return ds.Tables[0];
            return null;
        }

        public static DataTable layDanhKhoHang()
        {
            SqlDataAccessHelper.ConnectSQLServer();
            string procName = "sp_DanhSachKhoHang";
            DataSet ds = SqlDataAccessHelper.LoadData(procName);
            if (ds != null)
                return ds.Tables[0];
            return null;
        }

        public static bool capNhatKhoHang(KhoHang khohang)
        {
            SqlCommand cmd = SqlDataAccessHelper.CreateCommand("sp_ThemKhoHang");
            cmd.Parameters.Add("@makho", SqlDbType.Int).Value = khohang.Makho;
            cmd.Parameters.Add("@tenkho", SqlDbType.NVarChar).Value = khohang.Tenkho;
            cmd.Parameters.Add("@mota", SqlDbType.NVarChar).Value = khohang.Mota;
            cmd.Parameters.Add("@manv", SqlDbType.NVarChar).Value = khohang.Manv;

            return SqlDataAccessHelper.ExecCmd(cmd);
        }

        public static bool xoaKhoHang(KhoHang kh)
        {
            SqlCommand cmd = SqlDataAccessHelper.CreateCommand("sp_XoaKhoHang");
            cmd.Parameters.Add("@makho", SqlDbType.Int).Value = kh.Makho;
            return SqlDataAccessHelper.ExecCmd(cmd);
        }

        public static DataTable timKiemKhoHangTheoTen(string ten)
        {
            SqlDataAccessHelper.ConnectSQLServer();
            string procName = "sp_TraCuuKhoHangTheoTen";
            SqlParameter[] sp = new SqlParameter[1];
            sp[0] = new SqlParameter("@tenkho", ten);
            DataSet ds = SqlDataAccessHelper.LoadDataWithParameter(procName, sp);
            if (ds != null)
                return ds.Tables[0];
            return null;
        }
    }
}
